home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / DIRS.SWG / 0016_Find a file anywhere.pas < prev    next >
Pascal/Delphi Source File  |  1993-08-27  |  2KB  |  90 lines

  1. {
  2. PER-ERIC LARSSON
  3.  
  4. > I've seen some posts asking how to search through directories or how to
  5. > find a File anywhere on the disk, so here's a little Procedure I wrote
  6. > to do it...  Give it a whirl and feel free to ask questions...
  7.  
  8. There is a built in trap in the method you describe. I've fallen into it many
  9. times myself so here's a clue. The problem:
  10. if Your Procedure (that is called once per File) does some processing of the
  11. File you SHOULD first make a backup copy. personally I rename the original
  12. File to .BAK and then take that File as input, writing to a new File With the
  13. original name, perhaps deleting the .bak File if everything works out fine.
  14. For most purposes this works fine. But if you do this using findnext to find
  15. the next File to work With it will Repeat itself til the end of time or
  16. diskspace.
  17.  
  18. Therefore i recommend :
  19. First get all Filenames to work With,
  20. Then start processing the Files.
  21. }
  22.  
  23. Procedure runFile(ft : String);
  24. begin
  25.   { Process File here}
  26. end;
  27.  
  28. Procedure RUNALLFileS(FT : String);
  29. Type
  30.   plista = ^tlista;
  31.   tlista = Record
  32.     namn : String;
  33.     prev : plista;
  34.   end;
  35. Var
  36.  S    : SearchRec;
  37.  Dir  : DirStr;
  38.  Name : NameStr;
  39.  Ext  : ExtStr;
  40.  pp   : plista;
  41.  
  42. Function insertbefore(before : plista) : plista;
  43. Var
  44.   p : plista;
  45. begin
  46.   getmem(p, sizeof(tlista));
  47.   p^.prev := before;
  48.   insertbefore := p;
  49. end;
  50.  
  51. Function deleteafter(before : plista) : plista;
  52. begin
  53.   deleteafter := before^.prev;
  54.   freemem(before, sizeof(tlista));
  55. end;
  56.  
  57. begin
  58.   pp := nil;
  59.   FSplit(fT, Dir, Name, Ext);
  60.   FINDFIRST(ft, $3f, S);
  61.   While DosERROR = 0 DO
  62.   begin
  63.     if (S.ATTR and $18) = 0 then
  64.     begin
  65.       pp := insertbefore(pp);
  66.       pp^.namn := dir + s.name;
  67.    end;
  68.    FINDNEXT(S);
  69.   end;
  70.   if pp <> nil then
  71.   Repeat
  72.     runFile(pp^.namn);
  73.     pp := deleteafter(pp);
  74.   Until pp = nil;
  75. end;
  76.  
  77. begin
  78.   if paramcount > 0 then
  79.   begin
  80.     For filaa := 1 to paramcount do
  81.       runALLFileS(paramstr(filaa));
  82.   end;
  83.   Writeln('Klar')
  84. end.
  85.  
  86. {
  87. This is a cutout example from a Program i wrote
  88. It won't compile but it'll show a way to do it !
  89. }
  90.